package demo.util;

import java.util.ArrayList;
import java.util.Arrays;

public class CardIdUtil {
    /**
     * 验证身份证号是否有效
     *
     * @param cardId 身份证号
     * @return 身份证号是否有效
     */
    @Deprecated
    public static Boolean isValid(String cardId) {
        // 身份证号应为 18 位
        if (cardId.length() != 18)
            return false;
        final ArrayList<Integer> A = new ArrayList<>(Arrays.asList(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2));
        final ArrayList<Character> B = new ArrayList<>(Arrays.asList('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'));
        int sum = 0;
        for (int i = 0; i < A.size(); i++) {
            sum += Character.digit(cardId.charAt(i), 10) * A.get(i);
        }
        Character validLast = B.get(sum % 11);
        return cardId.charAt(17) == validLast;
    }

    /**
     * 隐匿身份证号的信息
     *
     * @param cardId 身份证号
     * @return 隐匿的身份证号信息
     */
    public static String anonymize(String cardId) {
        StringBuilder sb = new StringBuilder(cardId);
        return sb.replace(10, 18, "********").toString();
    }
}
